# Create your views here.
from opendemocracy.legislation.models import *
from django.http import HttpResponse, HttpResponseServerError, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.template import RequestContext
from django import forms





def all_problems(request):
    problems = Problem.objects.all()
    probText = []
    solutions = []
    for problem in problems:
        probText += ProblemText.objects.filter(problem=problem).order_by('-createdOn')[0:1]
        solutions += SolutionCase.objects.filter(problem=problem).order_by('-createdOn')[0:1]
    
    
    return render_to_response('template.html', locals(), context_instance=RequestContext(request))
    
    
    
def view_problem(request, problem_id):
    return True
    
def new_problem(request):
    post = request.POST.copy()
    user= request.user
    name = ""
    text = ""
    subject = ""
    if post.has_key('problem_name'):
        name = post['problem_name']
    if post.has_key('problem_text'):
        text= post['problem_text']
    if post.has_key('subject'):
        subjectName=post['subject']
    if Subject.objects.filter(name=subjectName):
        subject = Subject.objects.filter(name=subjectName)[0]
    else:
        subject = Subject.objects.create(createdBy=user, name=subjectName)
        
    newproblem = Problem.objects.create(createdBy=user, name=name, subject=subject)
    newproblemtext = ProblemText.objects.create(createdBy=user, problem=newproblem, text=text)

    return HttpResponseRedirect('/')

def edit_problem(request):
    post = request.POST.copy()
    user= request.user
    if post.has_key('problem_id'):
        problem_id = post['problem_id']
    if post.has_key('problem_text'):
        new_text = post['problem_text']
    if post.has_key('subject'):
        new_subject = post['subject']
    
    problem = Problem.objects.get(pk=problem_id)
    problemtext = ProblemText.objects.filter(problem=problem)[0]
    
    if problemtext.text != new_text:
        newproblemtext = ProblemText.objects.create(createdBy=user, problem=problem, text=new_text)
    if problem.subject.name != new_subject:
        problem.subject = Subject.objects.create(createdBy=user, name=new_subject)
        problem.save()
    
    return HttpResponseRedirect('/')


def new_solution(request):
    post = request.POST.copy()
    user = request.user
    name = ""
    text = ""
    if post.has_key('problem_id'):
        problemID = post['problem_id']
    if post.has_key('solution_name'):
        name = post['solution_name']
    if post.has_key('solution_text'):
        text = post['solution_text']
    
    problem = get_object_or_404(Problem, pk=problemID)
    
    solution = SolutionCase.objects.create(createdBy=user, problem=problem, name=name)
    solutionText = SolutionCaseText.objects.create(solutionCase=solution, createdBy=user, text=text)
    
    return HttpResponseRedirect('/')


    
def subjects(request):
    post = request.POST.copy()
    subjects = Subject.objects.all()
    
    
    resp ='['
    
    if len(subjects) >= 1:
        for subject in subjects:
            subjectEscapeName = subject.name.replace('"', '&quot;').replace('\n', '').replace('\r', '')
            subjectEscapeText = subject.text.replace('"', '&quot;').replace('\n', '').replace('\r', '')
            resp += '{ "id": "%s", "name": "%s", "text": "%s"}, ' % (subject.id, subjectEscapeName, subjectEscapeText)
        
        resp = resp[:-2]
        
    resp += ']'
    
    return HttpResponse(resp)
        

def new_problem_text(request):
    return True
